package discard;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @Author : 叶荻风
 * @create : 2024/7/3 16:15
 */
public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
    //DiscardServerHandler扩展 ChannelInboundHandlerAdapter，
    // 它是 ChannelInboundHandler 的实现。ChannelInboundHandler 提供了可以重写的各种事件处理程序方法。
    // 目前，扩展 ChannelInboundHandlerAdapter 就足够了，而不是自己实现处理程序接口

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg){
        // Discard the received data silently
        System.out.println((char) ((ByteBuf)msg).readByte());
        ((ByteBuf) msg).release();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){
        // Close the connection when an exception is raised
        cause.printStackTrace();
        ctx.close();
    }
}
